package adv5.exec;

import java.io.*;
import java.util.Arrays;

public class PictureVerify {

	public static void main(String[] args) {
		String fileName = "C:\\Users\\Administrator\\Desktop\\userface1.jpg";
		int ar[] = new int[8];
		try {
			File file = new File(fileName);
			FileInputStream f = new FileInputStream(file);
			for (int i = 0; i < ar.length; i++)
				ar[i] = f.read();
			// 读入8个字节
			f.close();
			// 获得文件的后缀
			String houzhui = file.getName().substring(file.getName().lastIndexOf('.') + 1);
			houzhui = houzhui.toLowerCase();

			switch (houzhui) {
			case "jpg":
				if (ar[0] == 0XFF && ar[1] == 0xd8) {
					System.out.println("合法的jpg文件");
				} else {
					System.out.println("非法的jpg文件");
				}
				break;
			case "png":
				int a2[] = { 0X89, 0X50, 0X4E, 0X47, 0X0D, 0X0A, 0X1A, 0X0A };
				boolean eq = Arrays.equals(ar, a2);
				if (eq) {
					System.out.println("合法的png文件");
				} else {
					System.out.println("非法的png文件");
				}
				break;
			case "gif":

				break;
			default:
				System.out.println("其他文件格式");
				break;
			}
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}

	}

}
